<?php
@header('Content-Type: text/html; charset=utf-8');
date_default_timezone_set("Asia/Shanghai");
include_once('dispatcher.php');

function smth_register($email, $keywords, $board, $reply_limit)
{
  $default_status="active";
  $default_start_time = 0;
  $source = "smth";

  $mysql = new mysqli("localhost", "ss", "ss", "db_subscribe");
  $mysql->set_charset("utf8");
  $ussi_insert_stmt = $mysql->prepare("insert into ss_user_info (nick,email,phone,status) values (?,?,?,?);");
  $ussi_select_stmt = $mysql->prepare("select uid from ss_user_info where email=?;");
  $uskw_insert_stmt = $mysql->prepare("insert into ss_user_keyword (uid,source,board,last_timestamp,keywords,reply_limit) values (?,?,?,?,?,?);");
  $uskw_update_stmt = $mysql->prepare("update ss_user_keyword set last_timestamp=?,keywords=?,reply_limit=? where uid=? and source=? and board=?;");

  $ussi_insert_stmt->bind_param("ssss", $email, $email, $email, $default_status);
  $ussi_insert_stmt->execute();
  $ussi_insert_stmt->close();

  $ussi_select_stmt->bind_param("s", $email);
  $ussi_select_stmt->execute();
  $ussi_select_stmt->bind_result($uid);
  $ussi_select_stmt->fetch();
  $ussi_select_stmt->close();

  $uskw_insert_stmt->bind_param("issisi", $uid, $source, $board, $default_start_time, $keywords,$reply_limit);
  $uskw_insert_stmt->execute();
  if (1 != $uskw_insert_stmt->affected_rows)
  {
    $uskw_update_stmt->bind_param("isiiss", $default_start_time, $keywords, $reply_limit, $uid, $source, $board);
    $uskw_update_stmt->execute();
    $uskw_update_stmt->close();
  }
  $uskw_insert_stmt->close();

  $mysql->close();
}

function smth_check_keywords($email, $keywords, $board, $reply_limit)
{
  $source = "smth";

  $mysql = new mysqli("localhost", "ss", "ss", "db_subscribe");
  $mysql->set_charset("utf8");
  $ussi_select_stmt = $mysql->prepare("select uid from ss_user_info where email=?;");
  $uskw_select_stmt = $mysql->prepare("select keywords,reply_limit from ss_user_keyword where uid=? and source=? and board=?;");

  $ussi_select_stmt->bind_param("s", $email);
  $ussi_select_stmt->execute();
  $ussi_select_stmt->bind_result($uid);
  $ussi_select_stmt->fetch();
  $ussi_select_stmt->close();

  $uskw_select_stmt->bind_param("iss", $uid, $source, $board);
  $uskw_select_stmt->execute();
  $uskw_select_stmt->bind_result($keywords4check, $reply_limit4check);
  $uskw_select_stmt->fetch();
  $uskw_select_stmt->close();

  $mysql->close();

  return ($keywords == $keywords4check
          && $reply_limit == $reply_limit4check);
}

function is_new_keywords($email, $board)
{
  $source = "smth";

  $mysql = new mysqli("localhost", "ss", "ss", "db_subscribe");
  $mysql->set_charset("utf8");
  $ussi_select_stmt = $mysql->prepare("select uid from ss_user_info where email=?;");
  $uskw_select_stmt = $mysql->prepare("select last_timestamp from ss_user_keyword where uid=? and source=? and board=?;");

  $ussi_select_stmt->bind_param("s", $email);
  $ussi_select_stmt->execute();
  $ussi_select_stmt->bind_result($uid);
  $ussi_select_stmt->fetch();
  $ussi_select_stmt->close();

  $uskw_select_stmt->bind_param("iss", $uid, $source, $board);
  $uskw_select_stmt->execute();
  $uskw_select_stmt->bind_result($last_timestamp);
  $uskw_select_stmt->fetch();
  $uskw_select_stmt->close();

  $mysql->close();

  return (0 == $last_timestamp);
}

function dispatch_one_user($email)
{
  global $user_select_sql;
  global $ussi_select_sql;
  global $ussd_select_ps;
  global $ussi_update_ps;

  $mysql = new mysqli("localhost", "ss", "ss", "db_subscribe");
  $mysql->set_charset("utf8");
  $ussd_select_stmt = $mysql->prepare($ussd_select_ps);
  $ussi_update_stmt = $mysql->prepare($ussi_update_ps);

  $result = $mysql->query($user_select_sql."'".$email."'");
  if ($row = $result->fetch_object())
  {
    $user_info = new UserInfo($row, $mysql, $ussi_select_sql);
  }

  $user_data = new UserData($user_info, $ussd_select_stmt);
  dispatch_user_data($user_data, $ussi_update_stmt);

  $mysql->close();
}

//dispatch_one_user("likai.root@gmail.com");
//smth_register("likai.root2@gmail.com", "hello world", "HoustRent");
?>
